package com.softmaker.controller;

import com.softmaker.entity.MangerInfo;
import com.softmaker.service.MangerInfoService;
import com.softmaker.utils.ResultVOUtil;
import com.softmaker.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/manger")
public class MangerController {
    @Autowired
    private MangerInfoService service;
    @PostMapping("/addManger")
    public Result<String> addUser(@RequestBody MangerInfo mangerInfo){
        System.out.println(mangerInfo.getMangerName().length());
        if(mangerInfo.getMangerName().length()<8 || mangerInfo.getMangerName().length()>12){
            return ResultVOUtil.error("用户名长度需要大于8位小于12位");
        }
        if(mangerInfo.getPassword().length()<8 || mangerInfo.getPassword().length()>12){
            return ResultVOUtil.error("密码长度需要大于8位小于12位");
        }
        if(service.findManger(mangerInfo)!=null){
            return ResultVOUtil.error("用户名已存在");
        }
        service.addManger(mangerInfo);
        return ResultVOUtil.success("成功");
    }
    @PostMapping("/getManger")
    public Result<MangerInfo> getUser(@RequestBody MangerInfo mangerInfo){
        MangerInfo mangerInfos = service.findManger(mangerInfo);

        if(mangerInfos == null){
            return ResultVOUtil.error("用户不存在");
        }
        if(!mangerInfo.getPassword().equals(mangerInfos.getPassword())){
            return ResultVOUtil.error("密码错误");
        }
        return ResultVOUtil.success(mangerInfos);
    }
    @PostMapping("/delManger")
    public Result<String> deleteUser(@RequestBody MangerInfo mangerInfo){
        if(mangerInfo == null || mangerInfo.getMangerName() == null || service.findManger(mangerInfo)==null){
            return ResultVOUtil.error("用户不存在");
        }
        service.deleteMangerInfoByMangerName(mangerInfo.getMangerName());
        return ResultVOUtil.success("成功");
    }
    @PostMapping("/updManger")
    public Result<MangerInfo> updateManger(@RequestBody MangerInfo mangerInfo){
        if(mangerInfo == null || mangerInfo.getMangerName() == null || service.findManger(mangerInfo)==null){
            return ResultVOUtil.error("用户不存在");
        }
        MangerInfo mangerInfo1=service.updatePasswordByMangerName(mangerInfo);
        return ResultVOUtil.success(mangerInfo1);
    }
    @GetMapping("/listManger")
    public Result<List<MangerInfo>> listUser(){
        return ResultVOUtil.success(service.listManger());
    }
}
